import axios from "axios";
const BASEURL = process.env.NODE_ENV==='production'?'':'/devApi'
import { Message } from 'element-ui';
import {getToken} from '@/utils/cookie'
const http = axios.create({
  timeout: 3000
});
//添加请求拦截器
http.interceptors.request.use(
  function(config) {
    //mockjs绕过代理
    if(!config.url.includes('mockApi')){
      config.url = BASEURL + config.url
    }
    //添加token
    config.headers.Tokey = getToken();
    config.headers.UserName = '903496672@qq.com';
    //在发送请求之前做某事
    return config;
  },
  function(error) {
    //请求错误时做些事
    return Promise.reject(error);
  }
);

//添加响应拦截器
http.interceptors.response.use(
  function(response) {
    //对响应数据做些事
    if(response.data.resCode===1040){
      Message.error(response.data.message)
    }else{
      return response.data;
    }
  },
  function(error) {
    //请求错误时做些事
    debugger
    return Promise.reject(error);
  }
);
export default http;
